package com.cedifie.lendapp.ui

import android.annotation.SuppressLint
import android.view.Gravity
import android.view.View
import android.webkit.WebChromeClient
import android.webkit.WebResourceRequest
import android.webkit.WebSettings
import android.webkit.WebView
import android.webkit.WebViewClient
import com.cedifie.lendapp.R
import com.cedifie.lendapp.config.CedConfigs
import com.cedifie.lendapp.databinding.ActPaPoyBinding
import com.cedifie.lendapp.http.HttpHelper
import com.cedifie.lendapp.mvp.RestartAct
import com.cedifie.lendapp.utils.event.CedEventType

class PaPoyAct : RestartAct() {

    private lateinit var binding: ActPaPoyBinding

    override fun getCedRootView(): View {
        binding = ActPaPoyBinding.inflate(layoutInflater)
        return binding.root
    }

    override fun initCedView() {
        setCedTitleBarVisibility(View.VISIBLE)
        setCedTextTitle(getString(R.string.privacy_policy_title))
        setCedTextTitleGravity(Gravity.CENTER)
        setCedRightBtnVisibility(View.GONE)

        initWebViewSettings()
    }

    override fun initCedListener() {
    }

    override fun initCedData() {
        binding.ppWebView.loadUrl(CedConfigs.CED_PRIVACY_POLICY_LINK)
    }

    @SuppressLint("SetJavaScriptEnabled")
    private fun initWebViewSettings() {
        binding.ppWebView.setInitialScale(100)
        binding.ppWebView.webChromeClient = mWebChromeClient
        binding.ppWebView.webViewClient = mWebClient
        val webSettings = binding.ppWebView.settings
        webSettings.setSupportZoom(true)
        try {
            webSettings.javaScriptEnabled = true
            webSettings.textZoom = 100
            webSettings.domStorageEnabled = true
            webSettings.builtInZoomControls = true
            webSettings.displayZoomControls = true
            webSettings.useWideViewPort = true
            webSettings.loadWithOverviewMode = true
            webSettings.cacheMode = WebSettings.LOAD_NO_CACHE
            webSettings.layoutAlgorithm = WebSettings.LayoutAlgorithm.SINGLE_COLUMN
            webSettings.mixedContentMode = WebSettings.MIXED_CONTENT_ALWAYS_ALLOW
            webSettings.allowUniversalAccessFromFileURLs = true
            webSettings.allowFileAccessFromFileURLs = true
            webSettings.allowContentAccess = true
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    private val mWebChromeClient = object : WebChromeClient() {
        override fun onProgressChanged(view: WebView?, newProgress: Int) {
            super.onProgressChanged(view, newProgress)

            binding.progressBar.progress = newProgress
            if (newProgress == 100) {
                binding.progressBar.visibility = View.GONE
            } else {
                binding.progressBar.visibility = View.VISIBLE
            }
        }
    }

    private val mWebClient = object : WebViewClient() {
        override fun shouldOverrideUrlLoading(view: WebView?, request: WebResourceRequest?, ): Boolean {
            return shouldOverrideUrlLoading(view, request?.url.toString())
        }

        @Deprecated("Deprecated in Java", ReplaceWith("false"))
        override fun shouldOverrideUrlLoading(view: WebView?, url: String?): Boolean {
            return false
        }
    }


    override fun onStart() {
        super.onStart()
        HttpHelper.addCedEventAction(CedEventType.PAGE.CED_PAGE_PRIVACY_POLICY_START,"")
    }

    override fun onDestroy() {
        super.onDestroy()
        HttpHelper.addCedEventAction(CedEventType.PAGE.CED_PAGE_PRIVACY_POLICY_END,"")
    }
}